package com.juxing.webserver.action.front;

import javax.servlet.http.HttpServletRequest;
import javax.servlet.http.HttpServletResponse;

import org.springframework.stereotype.Controller;
import org.springframework.web.bind.annotation.RequestMapping;
import org.springframework.web.bind.annotation.RequestParam;

import com.juxing.sdk.util.DigestUtil;
import com.juxing.sdk.util.StringUtil;
import com.juxing.webserver.action.BaseAction;
import com.juxing.webserver.util.VerifyCodeUtils;

/**
 * 非json类请求
 * @author brochen
 * @main mongibello@163.com
 */
@Controller
@RequestMapping("front/notJson")
public class FrontNotJsonAction extends BaseAction {

	/**
	 * 获取图形验证码图片
	 * @param request
	 * @param response
	 * @param imgCodeKey
	 * @param w
	 * @param h
	 * @throws Exception
	 */
	@RequestMapping("getImgCode")
	public void getImgCode(HttpServletRequest request, HttpServletResponse response
			,@RequestParam(required=true) String imgCodeKey		//图片验证码key
			,@RequestParam(required=false) Integer w		//图片宽度
			,@RequestParam(required=false) Integer h		//图片高度
			) throws Exception {
		if(w == null) {
			w = 0;
		}
		if(h == null) {
			h = 0;
		}
		if(w <= 0) {
			w = 200;
		}
		if(w > 800) {
			w = 800;
		}
		if(h<= 0) {
			h = 80;
		}
		if(h > 400) {
			h = 400;
		}
		if(StringUtil.isEmpty(imgCodeKey)) {
			return;
		}
		imgCodeKey = DigestUtil.decrypt3Des(imgCodeKey, configManager.getImgCodeDigestBytes());
		String[] cs = imgCodeKey.split("_");
		if(cs == null || cs.length != 3) {
			return;
		}
		String imgCode = cs[2];
		VerifyCodeUtils.outputImage(w, h, response.getOutputStream(), imgCode);
		response.flushBuffer();
	}
}
